/******************************************************************************中
 * @brief: file  : systask.c
 * @Author:   aple
 * @Date: 2022-12-14 18:06:21
 * @LastEditors: aple
 * @LastEditTime: 2023-02-27 16:06:54
 * @Verision: 1.0.0.0
 ******************************************************************************/
#define _SYSTASK_MODULE_
#include "os_obj.h"
#include "sys_task.h"
#include "pincfg.h"
#include "common.h"
#include "swap.h"
#include "nor_cmd.h"
#include "nand_w25n.h"
#include "bsp_api.h"
#include "fun_list.h"
#include "symbol_def.h"
#include "mem_alloc.h"
#include "bsp_spi_nor.h"
#include "bsp_io++.h"

/*****************************************************************************
 * @brief   fun.
 *****************************************************************************/
void sw_iic_opt(void);
void sys_flash_init(void);
void sys_led_ctr(void);
void nor_flash_demo(void);
void sys_tick_exe(void);
void system_info(void);
void str_demo(void);
void sys_par_load(void);


/*****************************************************************************
 * @brief   var.
 *****************************************************************************/
class pin_ndef_class *pin_led1;
class pin_ndef_class *pin_led2;
	
/*****************************************************************************
 * @brief   fun.
 *****************************************************************************/
extern "C"
{
	void task_system(const void *argument);
	FUN_LIST_REF(nsys);
	FUN_LIST_REF(napp);	
}

/*****************************************************************************
 * @brief   system task.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void task_system(const void *argument)
{
	uint32_t tick;

	thread_create();

	sys_init();
	
	dbg_print("flash sr = %08X\n", FLASH->SR);
	
	app_timer_task_add(sys_tick_exe);
	
	pin_led1 = new pin_ndef_class(GPIOD, 13);
	pin_led2 = new pin_ndef_class(GPIOD, 14);
	
	while (1)
	{
		uu8 *buff;

		buff = (uu8*)mem_ccm.api->malloc(2048);

		fun_list_run_once(nsys);

		Gst.info.stack_used = stack_used();

		Gst.u_tick = app_tick_get();

		// 虚拟RTC
		copy_u8(&Gst.pulse, buff, sizeof(Gst.pulse));
		pulse_creat((timer_pulse_type *)buff, Gst.u_tick);
		copy_u8(buff, &Gst.pulse, sizeof(Gst.pulse));

		if (Gst.pulse.pulse_1s)
		{
			TimeRun(&Gst.rtc_time);
		}

		sys_app_run_once();

		tick = app_tick_get();
		Gst.bsp_dly_verify = app_tick_get() - tick;

		NOP1();
		osDelay(ms_ticks(10));
		NOP1();

		mem_ccm.api->free(buff);
	}
}

/*****************************************************************************
 * @brief   led control.
 *****************************************************************************/
obj_app_order(sys_led_ctr);
void sys_led_ctr(void)
{
	pin_led1->out((uu08)Gst.pulse.auto_cnt_10ms%300==150);
	pin_led2->out((uu08)Gst.pulse.auto_cnt_10ms%300==1);
}

/*****************************************************************************
 * @brief   flash opt.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
obj_init_order5(sys_flash_init);
void sys_flash_init(void)
{
	extern const spi_flash_hal_type spi_nor;
	
	nor_spi_init();

	nor_init(&spi_nor, 0);

	nor_id_read(Gst.flash.nor_id);

	dbg_print("flash id: %02X %02X %02X \n", Gst.flash.nor_id[0],Gst.flash.nor_id[1],Gst.flash.nor_id[2]);
}

/*******************************************************************************
 * @brief: nor test
 * @param argc
 * @param *argv
 * @return {*}
 ******************************************************************************/
int iic(int argc, char *argv[])
{
	uu8 buff[10];

	sw_iic_init(sw_iic);

	loop(8)
	buff[index] = index;

	sw_iic_chip_send_str(sw_iic, 0xA0, 8, buff,8);

	osDelay(ms_ticks(12));
	
	loop(8)
	buff[index] = 0;

	sw_iic_chip_rcv_str(sw_iic, 0xA0, 8, buff,8);

	dbg_u8_print("IIC READ:\n", buff, 8, 16);
	
	return 0;
}
DBG_FUN_EXPORT(iic);

/*******************************************************************************
 * @brief: nor test
 * @param argc
 * @param *argv
 * @return {*}
 ******************************************************************************/
int nor_test(int argc, char *argv[])
{
	uu8 *buff, er = 0, rd = 0, wr = 0;
	uu32 addr = 1 * 1024 * 1024, len = 128;

	// 操作方式
	if (argc >= 2)
	{
		char *str = argv[1];

		if (str[0] == 'e')
			er = 1;
		if (str[0] == 'r')
			rd = 1;
		if (str[0] == 'w')
			wr = 1;
	}
	else
	{
		er = 1;
		wr = 1;
		rd = 1;
	}

	// 操作地址
	if (argc >= 3)
	{
		addr = atoi(argv[2]) * 1024;
	}

	// 操作数量
	if (argc >= 4)
	{
		len = atoi(argv[3]);
	}

	// 操作
	buff =(uu8*) malloc(len + 20);

	if (er)
	{
		nor_block_erase(addr);
		dbg_print("erase flash : 0X%08X\n", addr);
	}

	if (wr)
	{
		loop(len)
			buff[index] = index;

		nor_write_multi(addr, len, buff);

		dbg_print("write flash : 0X%08X\n", addr);
	}

	if (rd)
	{
		loop(len)
			buff[index] = 0;

		nor_read(addr, len, buff);

		dbg_print("read flash : 0X%08X\n", addr);
		dbg_u8_print("NOR READ:\n", buff, len, 16);
	}

	free(buff);

	return 0;
}
DBG_FUN_EXPORT(nor_test);

/*****************************************************************************
 *
 * @brief   task switch hook.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void task_sw_hook(char pos)
{
}

/*****************************************************************************
 *
 * @brief   sys_par_load
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void sys_par_load(void)
{
	// sys_par_flash_read();
	// if(sys_par.ver_id.uval != VERSION_ID)
	//{
	//	par_tab_init();
	//	sys_par_flash_save();
	// }
}

/*****************************************************************************
 *
 * @brief   sys_tick_exe
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
void sys_tick_exe(void)
{
}

/*****************************************************************************
 * @brief   system info.
 *
 * @ Pass/ Fail criteria: none
 *****************************************************************************/
obj_init_order4(system_info);
void system_info(void)
{

	dbg_puts(SYMBOL_LINE_EQ PTCR);

	dbg_puts(asc_pic_light);

	dbg_puts(SYMBOL_LINE_EQ PTCR);
	dbg_print("HCLK = %d MHZ, PCLK1 = %d MHZ, PCLK2 = %d MHZ\n",
			  HAL_RCC_GetHCLKFreq() / 1000000,
			  HAL_RCC_GetPCLK1Freq() / 1000000,
			  HAL_RCC_GetPCLK2Freq() / 1000000);
	dbg_puts(SYMBOL_LINE_EQ PTCR);
}

/*****************************************************************************
 * @brief   tick get.
 *
 * @ Pass/ Fail criteria: tick
 *****************************************************************************/
unsigned long rt_tick_get(void)
{
	return app_tick_get();
}

fun_list_hook(nsys)
{
}
